package com.py.longtrans.util;

import com.py.longtrans.LongTrans;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.TimerTask;

public class MonthTask extends TimerTask {

    // 每个月5号传上个月的
    private static String taskTime = "05";
    private static Logger logger = LoggerFactory.getLogger(MonthTask.class);
    @Override
    public void run() {
        logger.info("月传任务");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String s = df.format(new Date());// new Date()为获取当前系统时间
        String s1 = s.split(" ")[0]; // 2022-02-23
        String s2 = s1.substring(s1.lastIndexOf("-")+1,s1.length()); // 23
        ResourceBundle resource = ResourceBundle.getBundle("path");  // 读取自定义配置文件

        String filePath = resource.getString("path.monthout");

        SimpleDateFormat mf = new SimpleDateFormat("yyyyMM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
        date = calendar.getTime();
        String accDate = mf.format(date); // 上个月 202201

        String txtPath = filePath+accDate+"_00.txt";
        String chkPath = filePath+accDate+"_00.chk";

        if (s2.equals(taskTime)){
            logger.info("月传开始！");
//            SFTPUtil.uploadMonth();
            LongTrans.transFile(txtPath);
            LongTrans.transFile(chkPath);
        }

    }
}
